package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.adapter.ReceivingAddressAdapter;
import com.example.administrator.liveapp.bean.ReceivingAddressBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * 收货地址列表activity
 */

public class ReceivingAddressPage extends AppCompatActivity implements View.OnClickListener{

    private RecyclerView receiving_recycleView;
    private ImageView address_returns;//返回
    private TextView new_addition;//新增
    private TextView no_content;
    ReceivingAddressBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiving_address_page);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
    }

    private void initView() {
        receiving_recycleView = (RecyclerView) findViewById(R.id.receiving_recycleView);
        address_returns = (ImageView) findViewById(R.id.address_returns);
        new_addition = (TextView) findViewById(R.id.new_addition);
        no_content = (TextView) findViewById(R.id.no_content);
        address_returns.setOnClickListener(this);
        new_addition.setOnClickListener(this);
        initData();
    }

    private void initData() {
        String url = AppUrl.URL + AppUrl.orderAddress;
        Map<String,String> map = new HashMap<>();
        map.put("consumerId",ApplicationUtils.getInstance().loginBean.getConsumerid());
        XUtils.Get(url,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                bean = gson.fromJson(result,ReceivingAddressBean.class);
                if(bean.getCode()== 48002){
                    no_content.setText("你还没有收货地址!");
                    no_content.setVisibility(View.VISIBLE);
                    new_addition.setVisibility(View.VISIBLE);
                }else{
                    new_addition.setVisibility(View.GONE);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();

                recyclerView();
            }
        });
    }

    private void recyclerView() {
        FullyLinearLayoutManager linearLayoutManage = new FullyLinearLayoutManager(this);
        linearLayoutManage.setScrollEnabled(true);
        receiving_recycleView.setLayoutManager(linearLayoutManage);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        receiving_recycleView.setHasFixedSize(true);
        ReceivingAddressAdapter adapter = new ReceivingAddressAdapter(bean.getAddress());
        receiving_recycleView.setAdapter(adapter);
        adapter.setOnItemClickListener(new ReceivingAddressAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(ReceivingAddressPage.this,FillAddress.class);
                intent.putExtra("addressId",bean.getAddress().get(position).getAddressid());
                intent.putExtra("nickName",bean.getAddress().get(position).getNickname());
                intent.putExtra("telNumber",bean.getAddress().get(position).getTelnumber());
                intent.putExtra("orderAddress",bean.getAddress().get(position).getOrderaddress());
                intent.putExtra("detailAddress",bean.getAddress().get(position).getDetailaddress());
                intent.putExtra("addStatus",bean.getAddress().get(position).getAddstatus());
                intent.putExtra("token","1");
                startActivity(intent);
                overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.address_returns:
                finish();
                break;
            case R.id.new_addition:
                Intent intent = new Intent(this,FillAddress.class);
                intent.putExtra("token","2");
                startActivity(intent);
                overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                finish();
                break;
        }
    }
}
